﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using WcfService8;
using System.Reflection;

namespace Console_Host
{
    class Program
    {
        static void Main(string[] args)
        {
            //Uri httpUrl = new Uri("http://localhost:8090/MyService/SimpleCalculator");
            //ServiceHost host = new ServiceHost(typeof(WcfService8.Service_Console), httpUrl);
            //host.AddServiceEndpoint(typeof(WcfService8.IService_Console), new WSHttpBinding(), "");
            //ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            //smb.HttpGetEnabled = true;
            //host.Description.Behaviors.Add(smb);
            //host.Open();
            //Console.WriteLine("Service is host at " + DateTime.Now.ToString());
            //Console.WriteLine("Host is running... Press <Enter> key to stop");
            //Console.ReadLine();
            ServiceHost svcHost = new ServiceHost(typeof(WcfService8.Service_Console), new Uri("http://localhost:8001/MetadataSample"));
            try
            {
                ServiceMetadataBehavior smb = svcHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (smb == null)
                    smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                svcHost.Description.Behaviors.Add(smb);
                svcHost.AddServiceEndpoint(
                  ServiceMetadataBehavior.MexContractName,
                  MetadataExchangeBindings.CreateMexHttpBinding(),
                  "mex"
                );
                // Add application endpoint
                svcHost.AddServiceEndpoint(typeof(WcfService8.IService_Console), new WSHttpBinding(), "");
                // Open the service host to accept incoming calls
                svcHost.Open();

                // The service can now be accessed.
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();

                // Close the ServiceHostBase to shutdown the service.
                svcHost.Close();
            }
            catch (CommunicationException commProblem)
            {
                Console.WriteLine("There was a communication problem. " + commProblem.Message);
                Console.Read();
            }

        
        }
    }
}
